package com.zut.subject.application.controller;


import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zut.subject.application.convert.convertSubjectCategory;
import com.zut.subject.application.convert.convertSubjectLabel;
import com.zut.subject.application.dto.SubjectLabelDto;
import com.zut.subject.common.entity.Result;
import com.zut.subject.application.dto.SubjectCategoryDto;
import com.zut.subject.domain.entity.SubjectCategoryBo;
import com.zut.subject.domain.entity.SubjectLabelBo;
import com.zut.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 添加分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/add")
    private Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDto){
        Result result =new Result();
        try {

            if(log.isInfoEnabled()){
                log.info("subjectCategoryDomainService.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            //引入依赖guava，使用Google包下的Preconditions校验传入参数
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(),"前端传入分类类型为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDto.getCategoryName()),"您传入的名字为空");
            Preconditions.checkNotNull(subjectCategoryDto.getImageUrl(),"您传入的图片为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(),"您传入的父级id为空");

            //将dto类型转化为bo类型并传到domain层
            subjectCategoryDomainService.add(convertSubjectCategory.INSTANCE.toSubjectCategory(subjectCategoryDto));

            //成功返回true
            return result.success(true);
        } catch (Exception e) {
            //日志提示参数校验错误
            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e);
            return result.fail(e.getMessage());
        }
    }

    /**
     * 查询分类
     * @return
     */
    @PostMapping("/queryPrimaryCategory")
    private Result<List<SubjectCategoryDto>> queryCategory(@RequestBody SubjectCategoryDto subjectCategoryDto){
        Result result=new Result();

        try {
            //将传出的Dto类型转化为Bo类型传到domain层，domain层传过来的为bo类型
            SubjectCategoryBo subjectCategoryBo = convertSubjectCategory.INSTANCE.toSubjectCategory(subjectCategoryDto);
            List<SubjectCategoryBo> boList=subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBo);

            //将domain层传过来的bo类型转化为dto类型
            List<SubjectCategoryDto> dtoList=convertSubjectCategory.INSTANCE.toSubjectCategory(boList);
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController传出的数据为:{}", JSON.toJSONString(dtoList));
            }
            return result.success(dtoList);
        } catch (Exception e) {
            return result.fail(false);
        }
    }


    /**
     * 查询分类与标签优化
     * @return
     */
    @PostMapping("/queryCategoryAndLabel")
    private Result<List<SubjectCategoryDto>> queryCategoryAndLabel(@RequestBody SubjectCategoryDto subjectCategoryDto){
        Result result=new Result();

        try {
            //将传出的Dto类型转化为Bo类型传到domain层，domain层传过来的为bo类型
            SubjectCategoryBo subjectCategoryBo = convertSubjectCategory.INSTANCE.toSubjectCategory(subjectCategoryDto);
            List<SubjectCategoryBo> boList=subjectCategoryDomainService.queryCategoryAndLabel(subjectCategoryBo);
            //将domain层传过来的bo类型转化为dto类型
            List<SubjectCategoryDto> dtoList=convertSubjectCategory.INSTANCE.toSubjectCategory(boList);

            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController传出的数据为:{}", JSON.toJSONString(dtoList));
            }
            return result.success(dtoList);
        } catch (Exception e) {
            return result.fail(e.getMessage());
        }
    }



    /**
     * 查询大类下分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    private Result<List<SubjectCategoryDto>> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDto){
            Result result=new Result();
        try {
            //校验传入参数
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(),"前端传入分类类型为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(),"您传入的父级id为空");

            //将传出的Dto类型转化为Bo类型传到domain层，domain层传过来的为bo类型
            SubjectCategoryBo subjectCategoryBo = convertSubjectCategory.INSTANCE.toSubjectCategory(subjectCategoryDto);
            List<SubjectCategoryBo> boList=subjectCategoryDomainService.queryPrimaryCategory(subjectCategoryBo);

            //将domain层传过来的bo类型转化为dto类型
            List<SubjectCategoryDto> dtoList=convertSubjectCategory.INSTANCE.toSubjectCategory(boList);

            //输出日志
            if(log.isInfoEnabled()){
                log.info("SubjectCategoryController传出的数据为:{}", JSON.toJSONString(dtoList));
            }
            return result.success(dtoList);
        } catch (Exception e) {

            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e);
            //返回校验参数时错误信息
            return result.fail(e.getMessage());
        }
    }


    /**
     * 修改分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/update")
    private Result<String> update(@RequestBody SubjectCategoryDto subjectCategoryDto){
        Result result =new Result();

        try {
            //判断传入id不能为空
            Preconditions.checkNotNull(subjectCategoryDto.getId(),"您传入的id为空");
            //将dto转化为bo传入domain层
            SubjectCategoryBo subjectCategoryBo = convertSubjectCategory.INSTANCE.toSubjectCategory(subjectCategoryDto);

            //输出日志
            if (log.isInfoEnabled()){
                log.info("controller向domain传入数据为:{}",subjectCategoryBo);
            }
            boolean update = subjectCategoryDomainService.update(subjectCategoryBo);

            //根据update值判断是否修改成功，成功返回true，失败返回提示信息
            if (update==true)
            return result.success(update);
            else return result.fail("您输入的id有误！");

        } catch (Exception exception) {
            //失败返回失败信息
            return result.fail(exception.getMessage());
        }


    }

    /**
     * 删除分类
     */
    @PostMapping("/delete")
    private Result<String> delete(@RequestBody SubjectCategoryDto subjectCategoryDto){
        Result result =new Result();

        try {
            //判断传入id不能为空
            Preconditions.checkNotNull(subjectCategoryDto.getId(),"您传入的id为空");
            //将dto转化为bo传入domain层
            SubjectCategoryBo subjectCategoryBo = convertSubjectCategory.INSTANCE.toSubjectCategory(subjectCategoryDto);

            //输出日志
            if (log.isInfoEnabled()){
                log.info("controller向domain传入数据为:{}",subjectCategoryBo);
            }
            boolean update = subjectCategoryDomainService.delete(subjectCategoryBo);

            //根据update值判断是否修改成功，成功返回true，失败返回提示信息
            if (update==true)
                return result.success(update);
            else return result.fail("您输入的id有误！");

        } catch (Exception exception) {
            //失败返回失败信息
            return result.fail(exception.getMessage());
        }


    }


}
